package org.fjsei.yewu.job.inf;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;
import md.cm.unit.Unit;
import org.fjsei.yewu.job.PagenumJob;
import org.fjsei.yewu.job.SplitRepoJob;
import org.fjsei.yewu.job.util.Constant;
import org.fjsei.yewu.repository.StoreSync;
import org.fjsei.yewu.repository.StoreSyncRepository;
import org.fjsei.yewu.repository.maint.JsliceMang;
import org.fjsei.yewu.repository.maint.SplJob;
import org.fjsei.yewu.repository.maint.SplJobRepository;
import org.fjsei.yewu.service.third.InfRecvConvert;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.UnableToInterruptJobException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;
import java.util.function.Function;

@Slf4j
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
@Service
public class DeviceKeyJob extends InfRecvConvert implements PagenumJob {
    private volatile boolean interrupted = false; // 中断标志
    private JobKey jobKey;      // Job 的唯一标识
    @Autowired  protected SplJobRepository splJobRepository;
    private SplJob splJob;
    @Override
    public boolean isInterrupt() {
        return interrupted;
    }

    @Override
    public boolean executeSlice(JobKey  jobKey,SplJob  splJob) {
        this.jobKey=jobKey;
        Function<Long, String>  sliceJob= pageno ->{
            syncEqpFromLegacy_AK(pageno, splJob.getLimt());
            return null;
        };
        return runSlice(splJob, sliceJob,false);
    }

    @Override
    public SplJobRepository splJobRepo() {
        return splJobRepository;
    }

    @Override
    public void setSplJob(SplJob splJob) {
        this.splJob=splJob;
    }
    @Override
    public SplJob getSplJob() {
        return this.splJob;
    }

    @Override
    public void interrupt() throws UnableToInterruptJobException {
        interrupted = true; // 设置中断标志为 true
        log.info("Job with JobName {}, GroupName {} is started at {}", jobKey.getName(), jobKey.getGroup(), Constant.SIMPLE_DATE_FORMAT.format(new Date()));
    }
}
